"""doc"""

import json

from pprint import pprint

from rest_framework import status

from django.test import TestCase

from django.test import RequestFactory, TestCase, Client

from django.contrib.auth import (
    get_user_model,
)


# Create your tests here.
class HourseTest(TestCase):
    def setUp(self):
        # Every test needs access to the request factory.
        self.c = Client()
        self.factory = RequestFactory()
        self.admin_user = {"username": "admin", "password": "admin123"}
        self.err_admin_user = {"username": "admin", "password": "admin1234"}

        self.create_admin()

    def create_admin(self):
        # doc
        User = get_user_model()
        User.objects.create_superuser(email="myemail@test.com", **self.admin_user)

    # python manage.py test app_store_admin.tests.HourseTest
    def test_login_ok(self):
        # doc
        resp = self.c.post(
            "/api/admin/login",
            data=self.admin_user,
            content_type="application/json",
        )

        _content = json.loads(resp.content)

        _data = _content["data"]
        self.assertEqual(resp.status_code, status.HTTP_200_OK)
        self.assertEqual(_data.get("is_superuser"), True)
        self.assertEqual(_data.get("username"), self.admin_user["username"])

    def test_login_err(self):
        # doc
        resp = self.c.post(
            "/api/admin/login",
            data=self.err_admin_user,
            content_type="application/json",
        )

        _content = json.loads(resp.content)
        self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(_content.get("exception"), "AdminLoginFail, ")
